package com.ljf.daily.makemoneydetail.infra.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.infra.repository.impl.BasisRepositoryImpl;
import com.ljf.common.model.BasisModel;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.StringUtil;
import com.ljf.common.utils.WrapperUtil;
import com.ljf.daily.makemoneysoftware.domain.repository.DailyDatMakeMoneySoftwareRepository;
import com.ljf.daily.makemoneydetail.domain.repository.DailyDatMakeMoneyDetailRepository;
import com.ljf.daily.makemoneysoftware.dto.query.DailyDatMakeMoneySoftwareQuery;
import com.ljf.daily.makemoneydetail.dto.query.DailyDatMakeMoneyDetailQuery;
import com.ljf.daily.makemoneydetail.infra.mapper.DailyDatMakeMoneyDetailMapper;
import com.ljf.daily.makemoneysoftware.model.DailyDatMakeMoneySoftware;
import com.ljf.daily.makemoneydetail.model.DailyDatMakeMoneyDetail;
import com.ljf.daily.makemoneysoftware.vo.DailyDatMakeMoneySoftwareVO;
import com.ljf.daily.makemoneydetail.vo.DailyDatMakeMoneyDetailVO;
import com.ljf.finance.domain.repository.FinanceDatVirtualAccountRepository;
import com.ljf.finance.dto.query.FinanceDatVirtualAccountQuery;
import com.ljf.finance.vo.FinanceDatVirtualAccountVO;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 赚钱明细记录  数据访问接口实现
 *
 * @author LJF
 * @since 2024-01-25
 */
@Component
public class DailyDatMakeMoneyDetailRepositoryImpl extends BasisRepositoryImpl<DailyDatMakeMoneyDetailMapper, DailyDatMakeMoneyDetail, DailyDatMakeMoneyDetailQuery, DailyDatMakeMoneyDetailVO> implements DailyDatMakeMoneyDetailRepository {

    @Resource
    private DailyDatMakeMoneySoftwareRepository softwareRepository;

    @Resource
    private FinanceDatVirtualAccountRepository financeDatVirtualAccountRepository;

    @Override
    protected QueryWrapper<DailyDatMakeMoneyDetail> fillWrapper(DailyDatMakeMoneyDetailQuery query, boolean sort) throws ServiceException {
        QueryWrapper<DailyDatMakeMoneyDetail> wrapper = super.fillWrapper(query, false);
        wrapper.orderByDesc(DailyDatMakeMoneyDetail.COL_BELONG_DATE);
        wrapper.orderByDesc(BasisModel.COL_CREATE_TIME);
        if (StringUtil.isNotEmpty(query.getSoftwareRegisterPhone()) || StringUtil.isNotEmpty(query.getSoftwareNameLike()) || StringUtil.isNotEmpty(query.getSoftwareAccountCode())) {
            List<DailyDatMakeMoneySoftwareVO> softwareList = softwareRepository.selectList(
                    DailyDatMakeMoneySoftwareQuery.builder()
                            .registerPhone(query.getSoftwareRegisterPhone())
                            .nameLike(query.getSoftwareNameLike())
                            .accountCode(query.getSoftwareAccountCode())
                            .build()
            );
            if (CollectionUtils.isEmpty(softwareList)) {
                wrapper = WrapperUtil.getEmptyWrapper();
            }
            else {
                List<String> softwareCodeList = softwareList.stream().map(DailyDatMakeMoneySoftwareVO::getCode).distinct().collect(Collectors.toList());
                wrapper.in(DailyDatMakeMoneyDetail.COL_SOFTWARE_CODE, softwareCodeList);
            }
        }
        if (CollectionUtil.isNotEmpty(query.getIds())) {
            wrapper.in(DailyDatMakeMoneyDetail.COL_ID, query.getIds());
        }
        return wrapper;
    }

    @Override
    protected void fillInfo(List<DailyDatMakeMoneyDetailVO> resultList) throws ServiceException {
        super.fillInfo(resultList);
        if (CollectionUtil.isNotEmpty(resultList)) {
            List<String> softwareCodeList = resultList.stream().map(DailyDatMakeMoneyDetail::getSoftwareCode).distinct()
                    .filter(StringUtil::isNotEmpty).collect(Collectors.toList());

            List<DailyDatMakeMoneySoftwareVO> softwareList;
            if (CollectionUtil.isEmpty(softwareCodeList)) {
                softwareList = Collections.emptyList();
            }
            else {
                softwareList = softwareRepository.selectList(
                        DailyDatMakeMoneySoftwareQuery.builder().codeList(softwareCodeList).build()
                );
            }
            List<String> accountCodeList = softwareList.stream().map(DailyDatMakeMoneySoftwareVO::getAccountCode).distinct()
                    .filter(StringUtil::isNotEmpty).collect(Collectors.toList());
            Map<String, DailyDatMakeMoneySoftwareVO> softwareMap = softwareList.stream()
                    .collect(Collectors.toMap(DailyDatMakeMoneySoftwareVO::getCode, Function.identity(), (a, b) -> b));

            List<FinanceDatVirtualAccountVO> accountList;
            if (CollectionUtil.isEmpty(softwareCodeList)) {
                accountList = Collections.emptyList();
            }
            else {
                accountList = financeDatVirtualAccountRepository.selectList(
                        FinanceDatVirtualAccountQuery.builder().codeList(accountCodeList).build()
                );
            }
            Map<String, FinanceDatVirtualAccountVO> accountMap = accountList.stream()
                    .collect(Collectors.toMap(FinanceDatVirtualAccountVO::getCode, Function.identity(), (a, b) -> b));

            for (DailyDatMakeMoneyDetailVO result : resultList) {
                if (StringUtil.isNotEmpty(result.getSoftwareCode()) && softwareMap.containsKey(result.getSoftwareCode())) {
                    result.setSoftwareRegisterPhone(softwareMap.get(result.getSoftwareCode()).getRegisterPhone());
                    result.setSoftwareName(softwareMap.get(result.getSoftwareCode()).getName());
                    result.setSoftwareAccountCode(softwareMap.get(result.getSoftwareCode()).getCode());
                    result.setAccountName(accountMap.get(softwareMap.get(result.getSoftwareCode()).getAccountCode()).getName());
                }
            }

        }
    }

}
